//
//  NewsViewController.m
//  kr_36
//
//  Created by lmt on 16/1/18.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "NewsViewController.h"
#import "LMTAFNetwork.h"
#import "NewsCollectionViewCell.h"
#import "NewsDetailViewController.h"
#import "ActivityDetailController.h"
#import "SearchViewController.h"
@interface NewsViewController ()<UIScrollViewDelegate,UICollectionViewDataSource, UICollectionViewDelegate, passDelegate, passTwoDelegate>
@property (nonatomic, retain) UIScrollView *scroll;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSArray *buttonArr;/**< 按钮数组 */
@property (nonatomic, retain) NSArray *valueArr;/**< 对应value */
@end

@implementation NewsViewController

- (void)dealloc
{
    [_scroll release];
    [_buttonArr release];
    [_valueArr release];
    [_collectionView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"新闻";
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 创建标题scroll
    [self creatScroll];
    
    // 创建collectionView
    [self creatCollectionView];
    
    // 创建搜索按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemSearch) target:self action:@selector(search)];
    
    
    
}

#pragma mark - 跳转搜索页面
- (void)search
{
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
    [searchVC release];
}

#pragma mark - 标题scroll
- (void)creatScroll
{
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 40)];
    _scroll.showsHorizontalScrollIndicator = NO;
   // _scroll.backgroundColor = [UIColor redColor];
    // 按钮名字
    self.buttonArr = [NSArray arrayWithObjects:@"全部",  @"早期项目", @"B轮后", @"资本", @"深度", @"近期活动", nil];
    self.valueArr = [NSArray arrayWithObjects:@"all", @"67", @"68", @"69", @"70", @"activity", nil];
 
    _scroll.bounces = NO;
    
    NSInteger m = 0;
    
    // 在scroll上创建button按钮 并且在下方加横线
    for (NSInteger i = 0; i < _buttonArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        NSString *str = _buttonArr[i];
        [button setTitle:str forState:(UIControlStateNormal)];
        
        // button 字体大小
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        // button 字体颜色
        [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor blueColor] forState:(UIControlStateSelected)];
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17],};
        CGSize text = [str boundingRectWithSize:CGSizeMake(100, 100) options:(NSStringDrawingTruncatesLastVisibleLine) attributes:attributes context:nil].size;
        
       // NSLog(@"%fd", text.width);
        // 创建button 按坐标一次创建
        if (i == 0) {
            button.selected = YES;
            button.frame = CGRectMake(0, 0, text.width + 14, 39);
        } else {
            button.selected = NO;
           
            button.frame = CGRectMake(m, 0, text.width + 14, 39);
        }
        
        
        // button tag 值
        button.tag = 2500 + i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        
        [_scroll addSubview:button];
        
        // 横线
        UIView *xian = [[UIView alloc] initWithFrame:CGRectMake(m + 7, 39, text.width , 2)];
        xian.tag = 2600 + i;
        if (i == 0) {
            xian.backgroundColor = [UIColor blueColor];
        }
        [_scroll addSubview:xian];
        m = m + text.width + 14;
    }
    
    // scroll偏移量
    
    _scroll.contentSize = CGSizeMake(m, 40);
    
    [self.view addSubview:_scroll];
    [_scroll release];
}

#pragma mark - button点击
- (void)buttonAction:(UIButton *)btn
{
    // 先将所有的下划线和button颜色变为正常色
    for (NSInteger i = 0; i < _buttonArr.count; i++) {
        UIButton *a = [self.view viewWithTag:(i + 2500)];
         a.selected = NO;
        UIView *b = [self.view viewWithTag:(i + 2600)];
        b.backgroundColor = [UIColor whiteColor];
    }
    // 将点击的地方颜色发生变换
    UIView *c = [self.view viewWithTag:(btn.tag + 100)];
    c.backgroundColor = [UIColor blueColor];
    btn.selected = YES;
    btn.backgroundColor = [UIColor whiteColor];
    
    // collectionView 跟随点击发生变换
    //[_collectionView setContentOffset:CGPointMake(_collectionView.frame.size.width * (btn.tag - 2500), 0) animated:YES];
    NSIndexPath *i = [NSIndexPath indexPathForItem:btn.tag - 2500 inSection:0];
    [_collectionView scrollToItemAtIndexPath:i atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    [_collectionView reloadData];

}
#pragma mark - 创建collectionView
- (void)creatCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 大小
    layout.itemSize = CGSizeMake(self.view.frame.size.width , self.view.frame.size.height - 104 - 49 - 10);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(5, 0, 5, 0);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 104, self.view.frame.size.width, self.view.frame.size.height - 104 - 49) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[NewsCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_collectionView];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.pagingEnabled = YES;
    [_collectionView release];
    [layout release];
}

#pragma mark - collectionView方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _buttonArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NewsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    // 传一个值 解析数据
    cell.asd = _valueArr[indexPath.item];
  
    // 设置跳转页面代理人
    cell.delegate = self;
    cell.dataSource = self;
    return cell;
}

#pragma mark - 滑动button跟随方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == _collectionView) {
        NSInteger i = _collectionView.contentOffset.x / _collectionView.frame.size.width;
        
        // 所有下划线和按钮颜色变为正常
        for (NSInteger i = 0; i < _buttonArr.count; i++) {
            UIButton *a = [self.view viewWithTag:(i + 2500)];
            a.selected = NO;
            UIView *b = [self.view viewWithTag:(i + 2600)];
            b.backgroundColor = [UIColor whiteColor];
        }
        
        // 改变点击的按钮颜色
        UIView *c = [self.view viewWithTag:(i + 2600)];
        c.backgroundColor = [UIColor blueColor];
        UIButton *d = [self.view viewWithTag:(i + 2500)];
        d.selected = YES;
        d.backgroundColor = [UIColor whiteColor];
        
    }
}

- (void)passValue:(NSString *)str
{
    NewsDetailViewController *ndVC = [[NewsDetailViewController alloc] init];
    ndVC.str = str;
    
    [self.navigationController pushViewController:ndVC animated:YES];
}
- (void)passTwoValue:(NSString *)str
{
    ActivityDetailController *adVC = [[ActivityDetailController alloc] init];
    adVC.str = str;
    [self.navigationController pushViewController:adVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
